package net.mzzo.rc.util.location;

import net.mzzo.rc.util.http.HttpAction;

import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

public class TelephoneLocation {

	public static String getLocation(Context context) {
		try {
			TelephonyManager tManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			int type = tManager.getNetworkType();
			if (type == TelephonyManager.NETWORK_TYPE_GPRS
					|| type == TelephonyManager.NETWORK_TYPE_EDGE
					|| type == TelephonyManager.NETWORK_TYPE_HSDPA
					|| type == TelephonyManager.NETWORK_TYPE_UMTS) {
				return getGsmLocation(context);
			} else if (type == TelephonyManager.NETWORK_TYPE_CDMA
					|| type == TelephonyManager.NETWORK_TYPE_1xRTT
					|| type == TelephonyManager.NETWORK_TYPE_EVDO_A
					|| type == TelephonyManager.NETWORK_TYPE_EVDO_0) {
				return getCdmaLocation(context);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getCdmaLocation(Context context) {
		try {
			TelephonyManager mTManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			CdmaCellLocation cdma = (CdmaCellLocation) mTManager
					.getCellLocation();
			if (cdma == null)
				return null;
			double lat = (double) cdma.getBaseStationLatitude() / 14400;
			double lng = (double) cdma.getBaseStationLongitude() / 14400;

			return lng + "-" + lat;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getGsmLocation(Context context) {
		try {
			TelephonyManager mTManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			GsmCellLocation gsm = (GsmCellLocation) mTManager.getCellLocation();
			if (gsm == null)
				return null;
			int cid = gsm.getCid();
			int lac = gsm.getLac();
			String mcc = mTManager.getNetworkOperator().substring(0, 3);
			String mnc = mTManager.getNetworkOperator().substring(3, 5);

			// create json
			JSONObject jObject = new JSONObject();
			jObject.put("version", "1.1.0");
			jObject.put("host", "maps.google.com");
			jObject.put("request_address", true);
			jObject.put("home_mobile_country_code", mcc);
			jObject.put("home_mobile_network_code", mnc);
			jObject.put("radio_type", "gsm");
			jObject.put("request_address", true);
			if ("460".equals(mcc)) {
				jObject.put("address_language", "zh_CN");
			} else {
				jObject.put("address_language", "en_US");
			}
			JSONArray jArray = new JSONArray();
			JSONObject jData = new JSONObject();
			jData.put("cell_id", cid);
			jData.put("location_area_code", lac);
			jData.put("mobile_country_code", mcc);
			jData.put("mobile_network_code", mnc);
			jData.put("age", 0);
			jData.put("signal_strength", -60);
			jData.put("timing_advance", 5555);
			jArray.put(jData);
			jObject.put("cell_towers", jArray);
			Log.e("Location send", jObject.toString());

			// create connection, send request and receive response.
			JSONObject data = new HttpAction().JPost("http://www.google.com/loc/json", jObject);
			return String.valueOf(data.get("longitude")) + "-"
					+ String.valueOf(data.get("latitude"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}